<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/27
 * Time: 15:59
 */
namespace app\api\controller\v1;

use app\api\service\UserToken;
use app\api\validate\TokenGet;
use app\common\exception\ApiException;
use app\lib\exception\ParemeterException;
use app\api\service\Token as TokenService;
use think\facade\Request;

class Token {

    /**
     * 获取Thoken
     * @param string $code
     *
     * @return \think\response\Json
     * @throws \app\lib\exception\ParemeterException
     * @throws \app\lib\exception\WeChatException
     * @throws \think\Exception
     */
    public function getToken($code = ''){
        (new TokenGet())->goCheck();
        $ut = new UserToken($code);
        $token = $ut->get();
        $data = ['token'=>$token];
        return show(0,'获取成功',$data);
    }


//    验证token是否有正确
    public function veirfyToken(){
        $token = Request::header('token');
        if(!$token){
            throw  new ApiException(['msg'=>'token不允许为空']);
        }
        $valid = TokenService::verifyToken($token);
        return show(0,'验证成功',['isValid'=> $valid]);
    }

}